package com.cote.douhahou.pretloan.msg;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.cote.douhahou.pretloan.beans.AccountChaAkomapalendnnel;
import com.cote.douhahou.pretloan.launcher.BaseContext;
import com.cote.douhahou.pretloan.network.HttpHelper;
import com.cote.douhahou.pretloan.R;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;


public class AccoAkomapalenduntChannelViewAdapter extends RecyclerView.Adapter<AccoAkomapalenduntChannelViewAdapter.AccountChannelHolder_akomapalend> {

    private final ArrayList<AccountChaAkomapalendnnel> channels_akomapalend;

    private final String page_akomapalend;

    private AccountChaAkomapalendnnel selectChannel_akomapalend;

    private final ArrayList<AccountChannelHolder_akomapalend> holderList_akomapalend = new ArrayList<>();

    public AccoAkomapalenduntChannelViewAdapter(ArrayList<AccountChaAkomapalendnnel> channels, String page){
        this.channels_akomapalend = channels;
        this.page_akomapalend = page;
    }

    public AccountChaAkomapalendnnel getSelectChannel_akomapalend() {
        return selectChannel_akomapalend;
    }

    @NonNull
    @Override
    public AccountChannelHolder_akomapalend onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AccountChannelHolder_akomapalend(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_channel,parent,false));
    }

    @SuppressLint("InflateParams")
    @Override
    public void onBindViewHolder(@NonNull AccountChannelHolder_akomapalend holder, int position) {

        if (!holderList_akomapalend.contains(holder)) holderList_akomapalend.add(holder);

        AccountChaAkomapalendnnel channel = channels_akomapalend.get(position);

        holder.setChannelDesc_akomapalend(channel.getDesc_akomapalend());

        holder.setClick_akomapalend(v -> {
            HttpHelper.inst().submitEvent(page_akomapalend,"DIALOG_TYPE_CLICK",channel.getDesc_akomapalend());
            selectChannel_akomapalend = channel;
            for (AccountChannelHolder_akomapalend holder1: holderList_akomapalend){
                holder1.setCheck_akomapalend(holder1 == holder);
            }
        });

        if (channel.getBitmap_akomapalend()==null){
            new Thread(() -> {
                try {
                    if (TextUtils.isEmpty(channel.getLogo_akomapalend())) return;
                    URL url = new URL(channel.getLogo_akomapalend());
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setRequestMethod("GET");
                    if (connection.getResponseCode()==200){
                        InputStream inputStream = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        channel.setBitmap_akomapalend(bitmap);
                        new Handler(Looper.getMainLooper()).post(() -> holder.setLogo_akomapalend(bitmap));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }else holder.setLogo_akomapalend(channel.getBitmap_akomapalend());

    }

    @Override
    public int getItemCount() {
        return channels_akomapalend.size();
    }

    public static class AccountChannelHolder_akomapalend extends RecyclerView.ViewHolder{

        private final View bgView;
        private final ImageView checkIv;
        private final ImageView logoIv;
        private final TextView channelTv;

        @SuppressLint("InflateParams")
        public AccountChannelHolder_akomapalend(View view){

            super(view);

            bgView = view.findViewById(R.id.iacfa_view_bg);
            checkIv = view.findViewById(R.id.iacfa_iv_check);
            logoIv = view.findViewById(R.id.iacfa_iv_logo);
            channelTv = view.findViewById(R.id.iacfa_tv_channel);

        }

        public void setClick_akomapalend(View.OnClickListener clickListener){
            if (bgView !=null) bgView.setOnClickListener(clickListener);
        }

        public void setCheck_akomapalend(boolean isCheck){
            if (isCheck){
                checkIv.setVisibility(View.VISIBLE);
                bgView.setBackgroundResource(R.drawable.back_purple_border);
                channelTv.setTextColor(BaseContext.instance().getColor(R.color.account_channel_check_word_light));
            }else {
                checkIv.setVisibility(View.GONE);
                bgView.setBackgroundResource(R.drawable.back_round_c_gray);
                channelTv.setTextColor(BaseContext.instance().getColor(R.color.account_channel_check_word_gray));
            }
        }

        public void setLogo_akomapalend(Bitmap bitmap){
            logoIv.setImageBitmap(bitmap);
        }

        public void setChannelDesc_akomapalend(String text){
            channelTv.setText(text);
        }

    }


}
